package edu.nf.vrs.vaccination.service;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import edu.nf.vrs.vaccination.dao.AreaDao;
import edu.nf.vrs.vaccination.dao.VaccinationDao;
import edu.nf.vrs.vaccination.dto.AreaDTO;
import edu.nf.vrs.vaccination.dto.VaccinationDTO;
import edu.nf.vrs.vaccination.dto.VaccinationPageDTO;
import edu.nf.vrs.vaccination.entity.Area;
import edu.nf.vrs.vaccination.entity.Vaccination;
import edu.nf.vrs.vaccination.exception.DataAccessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Author: @Wai
 * Date: 2022-3-29
 */
@DubboService(interfaceClass = VaccinationService.class,version = "1.0.0")
@Slf4j
public class VaccinationServiceImpl implements VaccinationService {

    @Autowired
    private AreaDao areaDao;
    @Autowired
    private VaccinationDao vaccinationDao;

    @Override
    public List<AreaDTO> listArea() {
        List<Area> areaList = areaDao.listArea();
        List<AreaDTO> dtoList = BeanUtil.copyToList(areaList, AreaDTO.class);
        return dtoList;
    }

    @Override
    public VaccinationPageDTO listVaccination(Integer areaId, Integer pageNum, Integer pageSize) {
        List<Vaccination> list = vaccinationDao.listVaccination(areaId, pageNum, pageSize);
        PageInfo<Vaccination> pageInfo = new PageInfo(list);
        VaccinationPageDTO pageDTO = BeanUtil.copyProperties(pageInfo, VaccinationPageDTO.class);
        return pageDTO;
    }

    @Override
    public VaccinationDTO getVaccinationById(Integer id) {
        Vaccination vaccination = vaccinationDao.getVaccinationById(id);
        VaccinationDTO vaccinationDTO = BeanUtil.copyProperties(vaccination, VaccinationDTO.class);
        return vaccinationDTO;
    }

    @Override
    public void saveVaccination(VaccinationDTO vaccinationDTO) {
        try {
            Vaccination vaccination = BeanUtil.copyProperties(vaccinationDTO, Vaccination.class);
            vaccinationDao.saveVaccination(vaccination);
        } catch (RuntimeException e) {
            log.error(e.getMessage(), e);
            throw new DataAccessException("保存接种点异常，请稍后重试");
        }
    }

    @Override
    public void updateVaccination(VaccinationDTO vaccinationDTO) {
        try {
            Vaccination vaccination = BeanUtil.copyProperties(vaccinationDTO, Vaccination.class);
            vaccinationDao.updateVaccination(vaccination);
        } catch (RuntimeException e) {
            log.error(e.getMessage(), e);
            throw new DataAccessException("更新接种点异常，请稍后重试");
        }
    }

}
